package com.pzl.controller;

import com.pzl.entity.Role;
import com.pzl.entity.User;
import com.pzl.service.RoleService;
import com.pzl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.sound.midi.SoundbankResource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * UserController class
 *
 * @Author panzhenglei
 * @Date 2019/6/8 6:44 PM
 */


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    /**
     * 搜索全部User
     * @return
     * @throws Exception
     */
    @RequestMapping("/list.do")
    public ModelAndView list() throws Exception {

        ModelAndView modelAndView = new ModelAndView();

        List<User> users = userService.findAll();

        modelAndView.addObject("userList", users);

        modelAndView.setViewName("user-list");

        return modelAndView;
    }

    /**
     * 在新建User界面显示全部的Role
     * @return
     */
    @RequestMapping("saveUI.do")
    public ModelAndView saveUI(){

        ModelAndView modelAndView = new ModelAndView();

        List<Role> role = roleService.findAll();

        modelAndView.addObject("role",role);

        modelAndView.setViewName("user-add");

        return modelAndView;
    }


    @RequestMapping("/save.do")
    public ModelAndView save(Long id, String username, String email, String password, String phoneNum, Long[] roleIds) throws Exception {

        ModelAndView modelAndView = new ModelAndView();

        User user = new User();

        user.setEmail(email);
        user.setId(id);
        user.setPassword(password);
        user.setPhoneNum(phoneNum);
        user.setUsername(username);

        userService.save(user,roleIds);

        List<User> users = userService.findAll();

        modelAndView.addObject("userList",users);

        modelAndView.setViewName("user-list");

        return modelAndView;
    }


    @RequestMapping("updateUI.do")
    public ModelAndView update(Long id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("id: "+id);
        User user = userService.findOne(id);
        System.out.println("user "+user);
        List<Role> roles = roleService.findAll();

        List<Role> selected_roles = user.getRoles();
        System.out.println("selected_roles "+selected_roles);
        HashMap<Role, String> roleMap = new HashMap<>();

        for (Role role1 : roles) {

            roleMap.put(role1,"0");

            if (selected_roles !=null&&selected_roles.size() !=0){
                for (Role role2 : selected_roles) {
                    if (role2.equals(role1)){
                        roleMap.put(role1,"1");
                        break;
                    }
                }
            }

        }

        modelAndView.addObject("role",roleMap);

        modelAndView.addObject("user",user);

        modelAndView.setViewName("user-update");

        return modelAndView;

    }


    @RequestMapping("/update.do")
    public ModelAndView update(Long id, String username, String email, String password, String phoneNum, Long[] roleIds) throws Exception {

        ModelAndView modelAndView = new ModelAndView();

        User user = new User();
        user.setEmail(email);
        user.setId(id);
        user.setPassword(password);
        user.setPhoneNum(phoneNum);
        user.setUsername(username);

        userService.update(user,roleIds);

        List<User> users = userService.findAll();

        modelAndView.addObject("userList",users);

        modelAndView.setViewName("user-list");

        return modelAndView;

    }

    @RequestMapping("/delete.do")
    public ModelAndView delete(Long id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        Long delete = userService.delete(id);

        List<User> users = userService.findAll();

        modelAndView.addObject("userList",users);

        modelAndView.setViewName("user-list");

        return modelAndView;


    }
}
